//
//  EvaluateViewController.m
//  wtshoppingcite
//
//  Created by TW on 14-11-11.
//  Copyright (c) 2014年 TW. All rights reserved.
//

#import "EvaluateViewController.h"
#import "JPScoreView.h"
#import "EvaluateTableView.h"
#import "NetServiseCommonData.h"
#import "UIUtility.h"
#import "EvaluateModel.h"

@interface EvaluateViewController ()<UITableViewEventsDelegate>
{
    EvaluateTableView *evaluateTableView;
    NSString *pageNum;
}
@end

@implementation EvaluateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _dataArray = [[NSMutableArray alloc]initWithCapacity:1];
    
    [self setTitleView:@"全部评价"];
    
    self.view.backgroundColor = UIColorFromRGB(0xdcdddd);
    
    evaluateTableView = [[EvaluateTableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight-64) style:UITableViewStylePlain];
    evaluateTableView.isNeedTorefresh = YES;
    evaluateTableView.eventsDelegate = self;
    evaluateTableView.isMore = YES;
    [self.view addSubview:evaluateTableView];
    
    pageNum = @"0";
    [self pullDown:evaluateTableView];
  
    
    // Do any additional setup after loading the view.
}

-(void)getEvaluateWithData:(BOOL)isMore{
    
    NSString *page = pageNum;
    NSString *pageSize = @"10";
    
    __weak EvaluateViewController *weak_self = self;
    NSMutableDictionary *listDic = [NSMutableDictionary dictionary];
    
    SetValueForKey(listDic, _productId, @"productId");
    SetValueForKey(listDic, page, @"page");
    SetValueForKey(listDic, pageSize, @"pageSize");
    
    
    [NetWorkService byRequestWithAPIPrefix:kBaseAppUrl
                              andAPISuffix:kevaluate
                             andHttpMethod:@"POST"
                             andParameters:listDic
                           andRequestCache:NO
                            andUseParamDic:YES
                       andPostDataEncoding:MKNKPostDataEncodingTypeJSON
                     andRequestFinishBlock:^(BYRequest *request, BYResponse *response) {
                         [evaluateTableView doneLoadingTableViewData];
                         if (response.isError) {
                             
                             DLog(@"error %@",response.errorMessage);
                             
                         }else{
                             
                             NSDictionary *responseDic = request.responseJSON;
                             DLog(@"成功数据:%@",responseDic);
                             
                             NSArray *list = [responseDic objectForKey:@"list"];
                             
                             if (list.count < 10) {
                                 evaluateTableView.isMore = NO;
                             }else{
                                 evaluateTableView.isMore = YES;
                                 
                                 int currentPage = [pageNum intValue];
                                 pageNum = [NSString stringWithFormat:@"%d",currentPage+1];
                                 
                             }
                             if (!isMore) {
                                 [_dataArray removeAllObjects];
                             }
                             for (NSDictionary *dic in list) {
                                 EvaluateModel *evaluateModel = [[EvaluateModel alloc]initWithEvaluateInfo:dic];
                                 [_dataArray addObject:evaluateModel];
                             }
                             
                             evaluateTableView.data = _dataArray;
                         }
                         [evaluateTableView reloadData];
                     }];

}

#pragma mark  eventsDelegate
- (void)pullUp:(BaseTableView *)baseTableView{
    
    [self getEvaluateWithData:YES];
}

- (void)pullDown:(BaseTableView *)baseTableView{
    
    pageNum = @"0";
    [self getEvaluateWithData:NO];
    
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
}

-(void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
